package com.example.woke;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.bean.UserWallet;
import com.zhongcai.online.R;
import com.http.HttpMethods;
import com.woke.addressactivity.Userinfo_fenrunActivity;
import com.woke.method.MyApp;
import com.woke.serizedatas.Datas_load;
import com.wokeMy.view.turui.MyRealNameActivity;

import java.text.DecimalFormat;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class MywalletActivity extends Activity implements OnClickListener {

    //	private TextView mTextView;
    private int nextInt;
    private String user_money;
    private String user_proceeds;
    private TextView mTmoney;
    private MyApp application;
    private Datas_load loaded;
    private MyApp myapp;

    AlertDialog alertDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mywallet);
        myapp = (MyApp) getApplication();
        Intent intent = getIntent();
        user_money = intent.getStringExtra("user_money");
        user_proceeds = intent.getStringExtra("user_proceeds");
        intview();

    }

    private void intview() {
        application = (MyApp) getApplication();
        loaded = application.getDatas_load();
        findViewById(R.id.avmywallet_image_back).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        findViewById(R.id.avmywallet_relative_money).setOnClickListener(this);
        findViewById(R.id.avmywallet_relative_card).setOnClickListener(this);
        findViewById(R.id.avmywallet_relative_safeset).setOnClickListener(this);
        findViewById(R.id.avmywallet_relative_zhuanzhang).setOnClickListener(this);
        findViewById(R.id.avmywallet_relative_clear).setOnClickListener(this);
        findViewById(R.id.avmywallet_relative_fenrun).setOnClickListener(this);
        mTmoney = (TextView) findViewById(R.id.avmywallet_text_money);
        mTmoney.setText("￥" + user_money);
        DecimalFormat decimalFormat = new DecimalFormat(".00");
    }

    @Override
    protected void onResume() {
        super.onResume();
        getmywallet();
//		Thread t=new Thread(new MyRunnable());//���ﴴ���̶߳�����˸��������
//		t.start();

    }

    @Override
    public void onClick(View v) {

        if (null != application.getDatas_load().getUser_no_status()
                && application.getDatas_load().getUser_no_status()
                .equals("1")) {
//            Toast.makeText(MywalletActivity.this, "还未进行实名认证",
//                    Toast.LENGTH_SHORT).show();
            aralogid("还未实名认证", "前往认证？", 2);
        } else if (null != application.getDatas_load()
                .getUser_no_status()
                && application.getDatas_load().getUser_no_status()
                .equals("2")) {
            Toast.makeText(MywalletActivity.this, "实名认证审核中，无操作权限",
                    Toast.LENGTH_SHORT).show();
        } else {

            Intent intent = new Intent();

            switch (v.getId()) {
                case R.id.avmywallet_relative_money:
                    intent.setClass(MywalletActivity.this, MywalletonlvieActivity.class);
                    intent.putExtra("user_money", user_money);
                    intent.putExtra("user_proceeds", user_proceeds);
                    startActivity(intent);
                    break;

                case R.id.avmywallet_relative_card:
                    //			Random rd = new Random();
                    //			nextInt = rd.nextInt(2);
                    //			if(nextInt == 1){
                    //				intent.setClass(MywalletActivity.this,MywalletcardActivity.class);
                    //				startActivity(intent);
                    //			}else{
                    if (myapp.getDatas_load() == null)
                        return;
                    String state = myapp.getDatas_load().getUser_no_status();
                    if (TextUtils.isEmpty(state)) {
                        Toast.makeText(MywalletActivity.this, "还未进行实名认证",
                                Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if ("1".equals(state)) {
                        Toast.makeText(MywalletActivity.this, "还未进行实名认证",
                                Toast.LENGTH_SHORT).show();
                    } else if ("2".equals(state)) {
                        Toast.makeText(MywalletActivity.this, "实名认证审核中，无操作权限",
                                Toast.LENGTH_SHORT).show();
                    } else if ("3".equals(state)) {

                        intent.setClass(MywalletActivity.this, MybankblindActivity.class);
                        intent.putExtra("czhiortxian", "no");
                        //                    intent.setClass(MywalletActivity.this, BankCardListActivity.class);
                        startActivity(intent);
                    }

                    //			}

                    break;

                case R.id.avmywallet_relative_safeset:
                    intent.setClass(MywalletActivity.this, MywalletsafesetActivity.class);
                    startActivity(intent);
                    break;

                case R.id.avmywallet_relative_zhuanzhang:
                    intent.setClass(MywalletActivity.this, ZhuanActivity.class);
                    startActivity(intent);
                    break;

                case R.id.avmywallet_relative_clear:
                    intent.setClass(MywalletActivity.this, MywalletclearActivity.class);
                    startActivity(intent);
                    break;
                case R.id.avmywallet_relative_fenrun:
                    intent.setClass(MywalletActivity.this, Userinfo_fenrunActivity.class);
                    startActivity(intent);
                    break;
                default:
                    break;
            }
        }
    }

    private void getmywallet() {
        Datas_load datasLoad = application.getDatas_load();
        if (datasLoad != null) {
            String userLogin = datasLoad.getUser_login();
            if (!TextUtils.isEmpty(userLogin)) {
                HttpMethods.getInstance().userWallet(mWalletObserver, userLogin);
            }
        }
    }

    Observer<UserWallet> mWalletObserver = new Observer<UserWallet>() {
        @Override
        public void onSubscribe(Disposable d) {

        }

        @Override
        public void onNext(UserWallet value) {
            if (value != null) {
                String money_f = getString(R.string.user_money_f);
                mTmoney.setText(String.format(money_f, value.getUser_money()));
                String money_f2 = getString(R.string.user_money_f2);
                user_money = String.format(money_f2, value.getUser_money());
                user_proceeds = String.format(money_f2, value.getUser_proceeds());
            }
        }

        @Override
        public void onError(Throwable e) {

        }

        @Override
        public void onComplete() {

        }
    };
    private void aralogid(String info, String kll, final int ppp) {
        alertDialog = new AlertDialog.Builder(MywalletActivity.this).
                setTitle(info).
                setMessage(kll).
                setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent();
                        if (ppp == 1) {
                            intent.setClass (MywalletActivity.this,
                                    LoadActivity.class);
                            startActivity (intent);
                        } else if (ppp == 2) {
                            intent.setClass (MywalletActivity.this,
                                    MyRealNameActivity.class);
                            startActivity (intent);
                        }
                        alertDialog.dismiss ();
                    }
                }).
                setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        alertDialog.dismiss();
                    }
                }).create();
        alertDialog.show();
    }
}
